package cp7_jstl_javabean.jstl;

import bean.UserInfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DBTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/cp7/jstl/test1")
public class Test1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("nums", new int[]{1, 2, 3, 4});
        List<UserInfo> infoList = new ArrayList<>();
        try {
            Connection connection = DBTool.createConnection();
            QueryRunner qr = new QueryRunner();
            infoList = qr.query(connection, "select * from userinfo",
                    new BeanListHandler<UserInfo>(UserInfo.class));
            DBTool.closeConnection(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        req.setAttribute("userList", infoList);
        req.getRequestDispatcher("/cp7/jstl.jsp").forward(req, resp);
    }
}
